﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace TileSetConverter
{
    public partial class Form1 : Form
    {
        TileSet tileSet = null;
        bool usingPalette = false;
        string currentFilename = "";
        public Form1()
        {
            InitializeComponent();

            UpdateTitle();
        }
        void UpdateTitle()
        {
            if (string.IsNullOrEmpty(currentFilename))
                this.Text = "Dune 2000 Tile Set Editor";
            else
            {
                if(usingPalette)
                    this.Text = currentFilename + " R8 - " + "Dune 2000 Tile Set Editor";
                else
                    this.Text = currentFilename + " R16 - " + "Dune 2000 Tile Set Editor";
            }
        }


        private void loadTilesetButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Dune 2000 tileset files (*.r8; *.r16)|*.r8;*.r16";

            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                LoadTileSet(openDialog.FileName);
            }
        }

        private void LoadTileSet(string filename)
        {
            if (Path.GetExtension(filename).ToUpper() == ".R8")
            {
                OpenFileDialog openDialog = new OpenFileDialog();
                openDialog.FileName = "PALETTE.BIN";
                openDialog.Filter = "Dune 2000 palette files (*.bin)|*.bin";

                if (openDialog.ShowDialog() == DialogResult.OK)
                {
                    usingPalette = true;
                    tileSet = new TileSetR8(filename, openDialog.FileName);
                    UpdateTileSetPreview();
                    currentFilename = Path.GetFileNameWithoutExtension(filename);
                    UpdateTitle();
                }
            }
            else
            {
                usingPalette = false;
                tileSet = new TileSetR16(filename);
                UpdateTileSetPreview();
                currentFilename = Path.GetFileNameWithoutExtension(filename);
                UpdateTitle();
            }

            exportImageButton.Enabled = true;
            importImageButton.Enabled = true;
            saveTilesetButton.Enabled = true;
        }
        private void UpdateTileSetPreview()
        {
            Image image = (Image)new Bitmap(640, 1280);
            Graphics graphics = Graphics.FromImage(image);
            for (int x = 0; x < 20; x++)
            {
                for (int y = 0; y < 40; y++)
                {
                    graphics.DrawImage((Image)tileSet.Tiles[x + y * 20].bitmap, x * 32, y * 32);
                }
            }
            graphics.Dispose();
            previewBox.Image = image;
        }

        private void exportImageButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "PNG Image (*.png)|*.png";

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                previewBox.Image.Save(saveDialog.FileName, ImageFormat.Png);
            }
        }

        private void importImageButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "PNG Image (*.png)|*.png";

            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                ImportImage(openDialog.FileName);
            }
        }

        private void ImportImage(string filename)
        {
            Bitmap bitmap = new Bitmap(filename);
            if (bitmap.Width == 640 && bitmap.Height == 1280)
            {
                Cursor.Current = Cursors.WaitCursor;
                if (usingPalette)
                {
                    MessageBox.Show("Palette conversion will now take place.\n\nThis may take several minutes on slow machines");
                }
                for (int tileY = 0; tileY < 40; tileY++)
                {
                    for (int tileX = 0; tileX < 20; tileX++)
                    {
                        for (int y = 0; y < 32; y++)
                        {
                            for (int x = 0; x < 32; x++)
                            {
                                Color color;

                                if (usingPalette)
                                {
                                    color = bitmap.GetPixel(tileX * 32 + x, tileY * 32 + y);
                                    color = ((TileSetR8)tileSet).Palette.GetNearestColor(color);
                                }
                                else
                                {
                                    color = bitmap.GetPixel(tileX * 32 + x, tileY * 32 + y);
                                }
                                tileSet.Tiles[tileX + tileY * 20].bitmap.SetPixel(x, y, color);
                            }
                        }
                    }
                }
                Cursor.Current = Cursors.Default;
                UpdateTileSetPreview();
            }
            else
            {
                MessageBox.Show("Size must be 640x1280");
            }
        }

        private void saveTilesetButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.FileName = currentFilename;
            if(usingPalette)
                saveDialog.Filter = "R8 Image (*.R8)|*.R8";
            else
                saveDialog.Filter = "R16 Image (*.R16)|*.R16";

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                tileSet.Save(saveDialog.FileName);
            }
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("www.mvinetwork.co.uk/dune"); 
        }

        private void aboutButton_Click(object sender, EventArgs e)
        {
            AboutWindow window = new AboutWindow();
            window.ShowDialog();
        }
    }
}
